var t;

$(function() {
	$('#jtable').hide();
	$('#patientCreateFrm').submit(function() {
		var values = {};
		$.each($('#patientCreateFrm').serializeArray(), function(i, field) {
			values[field.name] = field.value;
		});
		var err = "";
		if ($.trim(values['firstName'])) {
			if ($.trim(values['lastName'])) {
				if ($.trim(values['appointmentDay'])) {
					return true;
				} else {
					err = "Appointment Day";
				}
			} else {
				err = "Last Name";
			}
		} else {
			err = "First Name";
		}
		$('#result').html('Err, ' + err + ' can not empty');
		$.showState();
		return false;
	});

	$('#patientEditFrm').submit(function() {
		var values = {};
		$.each($('#patientEditFrm').serializeArray(), function(i, field) {
			values[field.name] = field.value;
		});
		var err = "";
		if ($.trim(values['firstName'])) {
			if ($.trim(values['lastName'])) {
				return true;
			} else {
				err = "Last Name";
			}
		} else {
			err = "First Name";
		}
		$('#result').html('Err, ' + err + ' can not empty');
		$.showState();
		return false;
	});

});

$(document).ready(function() {
	$('form input[type=text]').val("");
	$('form textarea').val("");
	$("#datepicker").datepicker({
		dateFormat : 'dd-mm-yy',
		minDate : new Date()
	});
	$('#dialog-confirm').hide();
	$.jtabel();
	resfreshTimeOut();
	showCreateForm(0);
	$.showState();

});

function resfreshTimeOut() {
	refreshPaList();
	setTimeout('resfreshTimeOut()', 40000);
}

function refreshPaList() {
	$.ajax({
		url : './patientaction.do?act=getAll',
		dataType : "text",
		timeout : 30000,
		statusCode : {
			404 : function() {
				alert('page not found');
			}
		},
		success : function(data) {
			$('#jtable').show('slow');
			$('.loading').hide();
			$("#listPa").html("");
			var obj = jQuery.parseJSON(data);

			for (jobj in obj) {
				var item = obj[jobj];

				var id = item['idPatientDetail'];
				var dle = "<a class='edit' onclick='updatePatient(" + id
						+ ")'>Edit</a>";
				if (item['dle'] == 1) {
					dle = dle + "|<a class='edit'onclick='deletePatient(" + id
							+ ")'>Delete</a>";
				}

				var tr = '<tr><td>' + id + '</td><td>' + item['firstName']
						+ '</td><td>' + item['lastName'] + '</td><td>'
						+ item['emgContact'] + '</td><td>' + item['address']
						+ '</td><td>' + item['idUser'] + '</td><td>'
						+ dle + '</td></tr>';
				$("#listPa").append(tr);
				$.jtabel();
			}
		}
	});
}

function showCreateForm(i) {
	if (i == 0) {
		$('#patientEditFrm').hide();
		$('#patientCreateFrm').show();
	} else if (i == 1) {
		$('#patientEditFrm').hide('slow');
		$('#patientCreateFrm').show();
	} else {
		$('#patientEditFrm').show();
		$('#patientCreateFrm').hide('slow');
	}
}

function updatePatient(id) {
	$.ajax({
		url : './patientaction.do?act=getbyid&id=' + id,
		dataType : "text",
		timeout : 30000,
		statusCode : {
			404 : function() {
				alert('page not found');

			}
		},
		success : function(data) {
			var obj = jQuery.parseJSON(data);
			for (key in obj) {
				$('#' + key).attr('value', obj[key]);
			}

			$('#idPatient').attr('value', obj['idPatientDetail']);

			showCreateForm(2);
			refreshPaList();
		}
	});
}

function deletePatient(id) {

	$("#dialog:ui-dialog").dialog("destroy");

	$("#dialog-confirm").dialog(
			{
				resizable : false,
				modal : true,

				buttons : {
					"Delete" : function() {
						$(location).attr('href',
								'./patientaction.do?act=delete&id=' + id);
						$(this).dialog("close");
					},
					Cancel : function() {
						$(this).dialog("close");
					}
				}
			});

}
